<?php
namespace App\Service\Member;

class MySetService extends BaseService
{
    //我的设置

    /**
     * 收费列表
     */
    public function priceList($uid)
    {
        $arys = [
            'seek_cost' => ['title' => '向我咨询收费', 'price' => 0],
            'daiban_cost' => [],
        ];
        //咨询收费
        $dOne = (array) $this->dbConn($this->dbDoctor)
            ->where('uid', $uid)
            ->first();
        if (!empty($dOne) && $dOne['seek_price']) {
            $arys['seek_serve']['price'] = toNumber($dOne['seek_price']);
        }
        //代办收费
        $res = $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('uid', $uid)
            ->get();
        if (count($res)) {
            foreach ($res as $v) {
                $v = (array) $v;
                $arys['daiban_cost'][] = [
                    'id' => $v['cate_id'],
                    'title' => $v['cate_name'],
                    'price' => toNumber($v['price']),
                ];
            }
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 收费修改
     * @param int $uid
     * @param int $seek
     * @param array $daibans【id、title、price】
     */
    public function priceModify($uid, $seek, $daibans)
    {
        //咨询收费
        $dOne = (array) $this->dbConn($this->dbDoctor)
            ->where('uid', $uid)
            ->first();
        if (empty($dOne)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbDoctor)->where('uid', $uid)
            ->update(['seek_price' => $seek * 100]);
        $cateIds = [];
        if (count($daibans)) {
            foreach ($daibans as $v) {
                $dbOne = (array) $this->dbConn($this->dbUserve)
                    ->where('del', 0)
                    ->where('uid', $uid)
                    ->where('cate_id', $v['id'])
                    ->first();
                if (empty($dbOne)) {
                    //无则新增
                    $this->dbConn($this->dbUserve)->insertGetId([
                        'uid' => $uid, 'cate_id' => $v['id'],
                        'cate_name' => $v['title'], 'price' => $v['price'] * 100,
                        'addtime' => toDate(),
                    ]);
                } else {
                    //有则修改
                    $this->dbConn($this->dbUserve)->where('id', $dbOne)
                        ->update(['price' => $v['price'] * 100, 'uptime' => toDate()]);
                }
                $cateIds[] = $v['id'];
            }
        }
        //删除
        $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('uid', $uid)
            ->whereNotIn('cate_id', $cateIds)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return $this->toback();
    }

    /**
     * 代办列表
     * @param int $uid
     */
    public function daibanList($uid)
    {
        $arys = [
            'daiban_list' => [], 'yiyuan_list' => [],
        ];
        //代办列表
        $daibans = $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('type', 1)
            ->get();
        if (count($daibans)) {
            foreach ($daibans as $v) {
                $v = (array) $v;
                $arys['daiban_list'][] = [
                    'id' => $v['cate_id'],
                    'title' => $v['cate_name'],
                ];
            }
        }
        //医院列表
        $yys = $this->dbConn($this->dbUyiyuan)
            ->where('del', 0)
            ->where('uid', $uid)
            ->get();
        if (count($yys)) {
            foreach ($yys as $v) {
                $v = (array) $v;
                $arys['yiyuan_list'][] = [
                    'id' => $v['yy_id'],
                    'title' => $v['yy_name'],
                ];
            }
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 代办修改
     * @param int $uid
     * @param array $daibans【id、title】
     * @param array $yiyuans【id、title】
     */
    public function daibanModify($uid, $daibans, $yiyuans)
    {
        $type = 1;
        //代办服务
        $cateIds = [];
        if (count($daibans)) {
            foreach ($daibans as $v) {
                $res = (array) $this->dbConn($this->dbUserve)
                    ->where('del', 0)
                    ->where('type', $type)
                    ->where('uid', $uid)
                    ->where('cate_id', $v['id'])
                    ->first();
                if (empty($res)) {
                    //无则新增
                    $this->dbConn($this->dbUserve)->insertGetId([
                        'uid' => $uid, 'type' => $type,
                        'cate_id' => $v['id'],  'cate_name' => $v['title'],
                        'addtime' => toDate(),
                    ]);
                }
                $cateIds[] = $v['id'];
            }
        }
        //代办删除
        $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('type', $type)
            ->where('uid', $uid)
            ->whereNotIn('cate_id', $cateIds)
            ->update(['del' => 1, 'deltime' => toDate()]);
        //医院
        $yyids = [];
        if (count($yiyuans)) {
            foreach ($yiyuans as $v) {
                $yyOne = (array) $this->dbConn($this->dbUyiyuan)
                    ->where('del', 0)
                    ->where('uid', $uid)
                    ->where('yy_id', $v['id'])
                    ->first();
                if (empty($yyOne)) {
                    //无则新增
                    $this->dbConn($this->dbUyiyuan)->insertGetId([
                        'uid' => $uid, 'yy_id' => $v['id'],
                        'yy_name' => $v['title'], 'addtime' => toDate(),
                    ]);
                }
                $yyids[] = $v['id'];
            }
        }
        //医院删除
        $this->dbConn($this->dbUyiyuan)
            ->where('del', 0)
            ->where('uid', $uid)
            ->whereNotIn('yy_id', $yyids)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return $this->toback();
    }

    /**
     * 医护列表
     */
    public function yihuList($uid)
    {
        $arys = [
            'yihu_list' => [], 'yihu_time' => '',
        ];
        //医护服务
        $daibans = $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('type', 2)
            ->get();
        if (count($daibans)) {
            foreach ($daibans as $v) {
                $v = (array) $v;
                $arys['yihu_list'][] = [
                    'id' => $v['cate_id'],
                    'title' => $v['cate_name'],
                ];
            }
        }
        //可上门时间
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->first();
        $arys['yihu_time'] = !empty($uOne)&&$uOne['yihu_time'] ? toNumber($uOne['yihu_time']) : '';
        return $this->toback(0, '', $arys);
    }

    /**
     * 医护修改
     */
    public function yihuModify($uid, $yihus, $yytime)
    {
        $type = 2;
        //医护服务
        $cateIds = [];
        if (count($yihus)) {
            foreach ($yihus as $v) {
                $res = (array) $this->dbConn($this->dbUserve)
                    ->where('del', 0)
                    ->where('type', $type)
                    ->where('uid', $uid)
                    ->where('cate_id', $v['id'])
                    ->first();
                if (empty($res)) {
                    //无则新增
                    $this->dbConn($this->dbUserve)->insertGetId([
                        'uid' => $uid, 'type' => $type,
                        'cate_id' => $v['id'],  'cate_name' => $v['title'],
                        'addtime' => toDate(),
                    ]);
                }
                $cateIds[] = $v['id'];
            }
        }
        //删除医护
        $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('type', $type)
            ->where('uid', $uid)
            ->whereNotIn('cate_id', $cateIds)
            ->update(['del' => 1, 'deltime' => toDate()]);
        //可上门时间
        $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->update(['yihu_time' => $yytime]);
        return $this->toback();
    }

    /**
     * 新增自定义医护
     */
    public function yihuAdd($pas)
    {
        $type = 2; //小区医护
        $res = (array) $this->dbConn($this->dbUserve)
            ->where('del', 0)
            ->where('type', $type)
            ->where('cate_name', $pas['cate_name'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '已存在同名服务');
        }
        $pas['type'] = $type;
        $pas['cate_id'] = 0;
        $pas['uptime'] = $pas['addtime'] = toDate();
        $up = $this->dbConn($this->dbUserve)->insertGetId($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 陪诊列表
     */
    public function peizhenList($uid)
    {
        $arys = [
            'yiyuan_list' => [], 'peizhen_time' => '',
        ];
        //医院
        $res = $this->dbConn($this->dbUyiyuan)
            ->where('del', 0)
            ->where('uid', $uid)
            ->orderBy('id', 'asc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        foreach ($res as $v) {
            $v = (array) $v;
            $arys['yiyuan_list'][] = [
                'id' => $v['yy_id'],
                'title' => $v['yy_name'],
            ];
        }
        //陪诊时间
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->first();
        $arys['peizhen_time'] = !empty($uOne)&&$uOne['peizhen_time'] ? $uOne['peizhen_time'] : '';
        return $this->toback(0, '', $arys);
    }

    /**
     * 陪诊设置
     * @param int $uid
     * @param array $yys 医院数组
     * @param string $pztime 陪诊时间
     */
    public function peizhenModify($uid, $yys, $pztime)
    {
        //医院
        $yyids = [];
        if (count($yys)) {
            foreach ($yys as $v) {
                $yyOne = (array) $this->dbConn($this->dbUyiyuan)
                    ->where('del', 0)
                    ->where('yy_id', $v['id'])
                    ->first();
                if (empty($yyOne)) {
                    //无则添加
                    $this->dbConn($this->dbUyiyuan)->insertGetId([
                        'uid' => $uid, 'yy_id' => $v['id'],
                        'yy_name' => $v['title'], 'addtime' => toDate(),
                    ]);
                }
                $yyids[] = $v['id'];
            }
        }
        //删除医院
        $this->dbConn($this->dbUyiyuan)
            ->where('del', 0)
            ->where('uid', $uid)
            ->whereNotIn('yy_id', $yyids)
            ->update(['del' => 1, 'deltime' => toDate()]);
        //陪诊时间
        $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->update(['peizhen_time' => $pztime]);
        return $this->toback();
    }
}